﻿using System;
using System.Windows;
using System.Windows.Media;

namespace TMLibrary
{
    public static class UIHelper
    {
        public static void SetTimeout(int milliseconds, Action func)
        {
            var timer = new DispatcherTimerContainingAction
            {
                Interval = new TimeSpan(0, 0, 0, 0, milliseconds),
                Action = func
            };
            timer.Tick += _onTimeout;
            timer.Start();
        }
        private static void _onTimeout(object sender, EventArgs arg)
        {
            var t = sender as DispatcherTimerContainingAction;
            t.Stop();
            t.Action();
            t.Tick -= _onTimeout;
        }
    }
    public class DispatcherTimerContainingAction : System.Windows.Threading.DispatcherTimer
    {
         public Action Action { get; set; }
    }

    public static class TreeHelper
    {
        public static T FindParentByType<T>(this DependencyObject child) where T : DependencyObject
        {
            Type type = typeof(T);
            DependencyObject parent = VisualTreeHelper.GetParent(child);
            if (parent == null)
            {
                return null;
            }
            else if (parent.GetType() == type)
            {
                return parent as T;
            }
            else
            {
                return parent.FindParentByType<T>();
            }
        }
    }
}